package com.interest.business.utils;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Component
public class SmsService {

    @Autowired
    Client client;

    @Value("${aliyun.signName}")
    private String signName;

    @Value("${aliyun.templateCode}")
    private String templateCode;

    public JSONObject sendSmsCode(String phoneNumber,String code) throws Exception {

        Map<String, String> info = new HashMap<>();
        // 短信模板内容
        info.put("code", code);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber) // 手机号
                .setTemplateCode(templateCode) // 模板的Code
                .setSignName(signName) // 签名名称
                .setTemplateParam(JSONObject.toJSONString(info)); // 模板中的内容
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        return JSONObject.parseObject(JSONObject.toJSONString(sendSmsResponse.getBody()));
    }
}
